#include "binary_search.h"

int binary_search(int *p, int x, int sz)
{
	int right = sz - 1;
	int left = 0;
	int mid = (right + left) / 2;
	while (left <= right)
	{
		if (p[mid] > x)
		{
			right = mid - 1;
		}
		else if (p[mid] < x)
		{
			left = mid + 1;
		}
		if (p[mid] == x)
		{
			return mid;
		}
		mid = (right + left) / 2;
	}
	return -1;
}